package com.example.sj12;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

public class Setting extends AppCompatActivity {
    private Button btn;
    private EditText tv,tv1,tv2,tv3,tv4,tv5,tv6,tv7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Bundle b=getIntent().getExtras();
        //获取Bundle的信息
        final String username=b.getString("username");
        final String housename=b.getString("housename");
        //final String url=b.getString("url");
        findviews();
        insert(housename);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String housename1 = tv.getText().toString().trim();
                        String idcard = tv1.getText().toString().trim();
                        String tel = tv2.getText().toString().trim();
                        String price = tv3.getText().toString().trim();
                        String type = tv4.getText().toString().trim();
                        String area = tv5.getText().toString().trim();
                        String toward = tv6.getText().toString().trim();
                        String introduce = tv7.getText().toString().trim();
                        UserDao ud = new UserDao();
                        if(!ud.UpdateHouse(getIntent().getStringExtra("username"),housename,housename1,idcard,tel,price,type,area,toward,introduce)){
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(), "更新成功", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }

                        //以上为jdbc登录
                    }
                }).start();
            }
        });
    }

    private void insert(final String username) {
        //UserDao d = new UserDao();
        //final Data get = d.insert(username);
        new Thread() {
            @Override
            public void run() {
                super.run();
                UserDao d = new UserDao();
                final Data get = d.insert(username);
                Handler mHandler = new Handler(Looper.getMainLooper()) {

                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        // 切换到主线程
                        //DataAdapter mAdapter = new DataAdapter((LinkedList<Data>) mData, mContext);//初始化适配器对象
                        //list.setAdapter(mAdapter);//设置适配器
                        tv.setText(get.getHousename());
                        tv1.setText(get.getIdcard());
                        tv2.setText(get.getTel());
                        tv3.setText(get.getPrice());
                        tv4.setText(get.getType());
                        System.out.println("面积："+get.getArea());
                        tv5.setText(get.getArea());
                        tv6.setText(get.getToward());
                        tv7.setText(get.getIntroduce());
                    }
                };
                mHandler.sendEmptyMessage(0);
            }
        }.start();
    }

    private void findviews() {
        tv=findViewById(R.id.tv);
        tv1=findViewById(R.id.tv1);
        tv2=findViewById(R.id.tv2);
        tv3=findViewById(R.id.tv3);
        tv4=findViewById(R.id.tv4);
        tv5=findViewById(R.id.tv5);
        tv6=findViewById(R.id.tv6);
        tv7=findViewById(R.id.tv7);
        btn=findViewById(R.id.btn);
    }
}
